from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from selenium.common.exceptions import UnexpectedAlertPresentException

driver = webdriver.Chrome()
driver.get("http://www.helloweba.com/demo/2017/unlock/")

#定位滑块
slider = driver.find_elements_by_class_name("slide-to-unlock-handle")[2]
action = ActionChains(driver)
action.click_and_hold(slider).perform()
for index in range(200):
    try:
        action.move_by_offset(2,0).perform()
    except UnexpectedAlertPresentException:
        break
    action.reset_actions()
    sleep(0.1)    #等待停顿时间
#打印警告框提示
success_text = driver.switch_to.alert.text
print(success_text)

#click_and_hold()单击并按下鼠标左键
#move_by_offset()移动鼠标，第一个参数为x坐标距离，第二个参数为y坐标抽的距离
#reset_actions():重置action